import 'package:flutter_common_fragement/common/base/index.dart';

import 'root/page/hall.dart';
import 'root/page/message.dart';
import 'root/page/mine.dart';
import 'root/page/workbench.dart';

class RootViewModel extends BaseViewModel {
  DateTime? _lastPressedAt; // 上次点击时间

  Future<bool> willPop() async {
    toast('1秒内连续按两次返回键退出');
    if (_lastPressedAt == null ||
        DateTime.now().difference(_lastPressedAt!) >
            const Duration(seconds: 1)) {
      //两次点击间隔超过1秒则重新计时
      _lastPressedAt = DateTime.now();
      return false;
    }
    return true;
  }

  /// pageController 跟 底部导航默认
  final PageController _pageController = PageController();

  PageController get pageController => _pageController;

  int _currentIndex = 0;

  int get currentIndex => _currentIndex;

  void setCurrentIndex(int index) {
    if (index == _currentIndex) return;
    _currentIndex = index;
    _pageController.jumpToPage(index);
    notifyListeners();
  }

  /// 获取导航视图
  final List<Widget> navViews = [
    (WorkBenchPage()..setKeepAlive()).initWidget,
    (HallPage()..setKeepAlive()).initWidget,
    (MessagePage()..setKeepAlive()).initWidget,
    // WorkBenchPage().initWidget,
    // HallPage().initWidget,
    // Center(
    //   child: Text('消息'),
    // ),
    const MinePage(),
  ];

  final List<BottomNavigationBarItem> navBottomItems = const [
    BottomNavigationBarItem(icon: Icon(Icons.home), label: '工作台'),
    BottomNavigationBarItem(icon: Icon(Icons.hail), label: '接单大厅'),
    BottomNavigationBarItem(icon: Icon(Icons.messenger), label: '消息'),
    BottomNavigationBarItem(icon: Icon(Icons.person), label: '我的'),
  ];

  @override
  void viewModelInitForContext(BuildContext context) {
    // TODO: implement viewModelInitForContext
    super.viewModelInitForContext(context);

    /// 这里一般是更新app逻辑 做一些app内容的初始化
  }
}
